package com.kevin.mapping.script.lexer;

import com.kevin.mapping.script.engine.JSONEnv;

import java.math.BigDecimal;

public class ObjectValue extends Token {

    private Object value;

    public ObjectValue(int type, Object content) {
        super(type);
        this.value = content;
    }

    public String toString() {
        return "value=" + value + " type=" + (char) type;
    }

    public Object getValue(JSONEnv env) {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }


    @Override
    public Integer getIntValue(JSONEnv env) {
        if (value instanceof Integer) {
            return Integer.valueOf(value.toString());
        }
        return null;
    }

    @Override
    public Long getLongValue(JSONEnv env) {

        if (value instanceof Long) {
            return Long.valueOf(value.toString());
        }
        return null;
    }

    @Override
    public Double getDoubleValue(JSONEnv env) {
        if (value instanceof Long) {
            return Double.valueOf(value.toString());
        }
        return null;
    }

    @Override
    public BigDecimal getBigDecimalValue(JSONEnv env) {
        try {
            return new BigDecimal(value.toString());
        } catch (Exception e) {
            return null;
        }

    }

    @Override
    public String getStrValue(JSONEnv env) {
        return String.valueOf(value);
    }

    @Override
    public Object getObjectValue(JSONEnv env) {
        return value;
    }
}
